package com.yami.shop.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Advertising;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.AdvertisingMapper;
import com.yami.shop.service.AdvertisingService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxf
 * @date 2023/6/13 15:48
 */
@Service
@AllArgsConstructor
public class AdvertisingServiceImpl extends ServiceImpl<AdvertisingMapper, Advertising> implements AdvertisingService {

    private final AdvertisingMapper advertisingMapper;

    @Override
    public IPage<Advertising> getPage(PageParam<Advertising> page, Advertising param) {
        IPage<Advertising> pageVO = advertisingMapper.getPage(page, param);
        return pageVO;
    }

    @Override
    public void deleteAdvertisingByIds(Long[] ids) {
        advertisingMapper.deleteAdvertisingByIds(ids);
    }

    @Override
    public List<JSONObject> getAdvertising() {
        List<JSONObject> result = new ArrayList<>();
        List<Advertising> list = advertisingMapper.selectList(Wrappers.lambdaQuery(Advertising.class).eq(Advertising::getStatus, 0).orderByDesc(Advertising::getCreateTime));
        list.forEach(a ->{
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("headline",a.getHeadline());
            jsonObject.set("content",a.getContent());
            jsonObject.set("skipUrl",a.getSkipUrl());
            jsonObject.set("pic",a.getPic());
            result.add(jsonObject);
        });
        return result;
    }
}
